package br.com.unieuro.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import br.com.unieuro.dominio.Camera;

public class CameraDAO {

	String url = "jdbc:postgresql://127.0.0.1:5432/bd_sgi";
	String usuario = "postgres";
	String senha = "bolseir0";

	public void salvar(Camera camera){
		try{
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, usuario, senha);
			PreparedStatement st = con.prepareStatement("INSERT INTO item (ip, numCamera, posicionamento, vagasMonitoradas) VALUES (?,?,?,?)");
			st.setString(1, camera.getIp());
			st.setInt(2, camera.getNumCamera());
			st.setString(3, camera.getPosicionamento());
			st.setString(4, camera.getVagasMonitoradas());
			st.executeUpdate();
			st.close();
			con.close();
		}catch(SQLException erro){
			erro.printStackTrace();
		}catch(ClassNotFoundException erro){
			erro.printStackTrace();
		}
	}

	public ArrayList<Camera> listarCameras(){
		ArrayList<Camera> cams = new ArrayList<Camera>();
		try{
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, usuario, senha);
			Statement st = con.createStatement();
			ResultSet resultado = st.executeQuery("SELECT * FROM item");
			while(resultado.next()){
				Camera camera = new Camera();
				camera.setId(resultado.getInt("id"));
				camera.setIp(resultado.getString("ip"));
				camera.setNumCamera(resultado.getInt("numCamera"));
				camera.setPosicionamento(resultado.getString("posicionamento"));
				camera.setVagasMonitoradas( resultado.getString("vagasMonitoradas"));
				cams.add(camera);
			}
			st.close();
			con.close();
		}catch(SQLException erro){
			erro.printStackTrace();
		}catch(ClassNotFoundException erro){
			erro.printStackTrace();
		}
		return cams;
	}

	public void excluir(Camera camera){
		try{
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, usuario, senha);
			PreparedStatement st = con.prepareStatement("DELETE FROM camera WHERE id='"+camera.getId()+"'");
			st.executeUpdate();
			st.close();
			con.close();
		}catch(SQLException erro){
			erro.printStackTrace();
		}catch(ClassNotFoundException erro){
			erro.printStackTrace();
		}
	}

	public void update(Camera camera){
		try{
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, usuario, senha);
			PreparedStatement st = con.prepareStatement("UPDATE camera SET ip = ?, numCamera=?, posicionamento=?, vagasMonitoradas=? WHERE id=?");

			st.setString(1, camera.getIp());
			st.setInt(2, camera.getNumCamera());
			st.setString(3, camera.getPosicionamento());
			st.setString(4, camera.getVagasMonitoradas());
			st.setInt(5, camera.getId());

			st.executeUpdate();
			st.close();
			con.close();
		}catch(SQLException erro){
			erro.printStackTrace();
		}catch(ClassNotFoundException erro){
			erro.printStackTrace();
		}
	}
}
